<template>
  <div class="publish-container">
    <div class="page-header">
      <h1 class="page-title">
        <el-icon class="title-icon"><Plus /></el-icon>
        发布技能
      </h1>
      <p class="page-subtitle">分享你的技能，帮助他人成长</p>
    </div>

    <div class="form-container">
      <el-form :model="skillForm" :rules="rules" ref="formRef" label-width="100px">
        <el-form-item label="技能标题" prop="title">
          <el-input v-model="skillForm.title" placeholder="请输入技能标题" />
        </el-form-item>

        <el-form-item label="技能分类" prop="category">
          <el-select v-model="skillForm.category" placeholder="选择分类">
            <el-option label="学习类" :value="1" />
            <el-option label="生活类" :value="2" />
            <el-option label="艺术类" :value="3" />
            <el-option label="编程类" :value="4" />
            <el-option label="其他" :value="5" />
          </el-select>
        </el-form-item>

        <el-form-item label="封面图片">
          <el-upload
            class="cover-uploader"
            :show-file-list="false"
            :before-upload="beforeCoverUpload"
            :http-request="handleCoverUpload"
          >
            <img v-if="skillForm.coverImage" :src="skillForm.coverImage" class="cover-image">
            <el-icon v-else class="cover-uploader-icon"><Plus /></el-icon>
          </el-upload>
          <div class="upload-tip">建议尺寸: 800x600px，大小不超过5MB</div>
        </el-form-item>

        <el-form-item label="技能描述" prop="description">
          <el-input
            v-model="skillForm.description"
            type="textarea"
            :rows="5"
            placeholder="详细描述你的技能..."
          />
        </el-form-item>

        <el-form-item label="技能标签">
          <el-tag
            v-for="tag in tags"
            :key="tag"
            closable
            @close="removeTag(tag)"
            style="margin-right: 10px"
          >
            {{ tag }}
          </el-tag>
          <el-input
            v-if="inputVisible"
            ref="inputRef"
            v-model="inputValue"
            size="small"
            style="width: 100px"
            @keyup.enter="handleInputConfirm"
            @blur="handleInputConfirm"
          />
          <el-button v-else size="small" @click="showInput">+ 添加标签</el-button>
        </el-form-item>

        <el-form-item label="所需时间币">
          <el-input-number v-model="skillForm.timeCoins" :min="0" :step="10" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleSubmit" :loading="loading">
            发布技能
          </el-button>
          <el-button @click="router.back()">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, nextTick } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { publishSkill } from '@/api/skill'
import { uploadImage } from '@/api/file'

const router = useRouter()
const formRef = ref(null)
const inputRef = ref(null)
const loading = ref(false)
const inputVisible = ref(false)
const inputValue = ref('')
const tags = ref([])

const skillForm = reactive({
  title: '',
  category: null,
  description: '',
  tags: '',
  coverImage: '',
  timeCoins: 30
})

const rules = {
  title: [
    { required: true, message: '请输入技能标题', trigger: 'blur' }
  ],
  category: [
    { required: true, message: '请选择技能分类', trigger: 'change' }
  ],
  description: [
    { required: true, message: '请输入技能描述', trigger: 'blur' }
  ]
}

const showInput = () => {
  inputVisible.value = true
  nextTick(() => {
    inputRef.value?.focus()
  })
}

const handleInputConfirm = () => {
  if (inputValue.value && !tags.value.includes(inputValue.value)) {
    tags.value.push(inputValue.value)
  }
  inputVisible.value = false
  inputValue.value = ''
}

const removeTag = (tag) => {
  tags.value = tags.value.filter(t => t !== tag)
}

const beforeCoverUpload = (file) => {
  const isImage = file.type.startsWith('image/')
  const isLt5M = file.size / 1024 / 1024 < 5

  if (!isImage) {
    ElMessage.error('只能上传图片文件!')
    return false
  }
  if (!isLt5M) {
    ElMessage.error('图片大小不能超过 5MB!')
    return false
  }
  return true
}

const handleCoverUpload = async ({ file }) => {
  try {
    const res = await uploadImage(file)
    skillForm.coverImage = res.data.url
    ElMessage.success('封面上传成功')
  } catch (error) {
    console.error('封面上传失败：', error)
  }
}

const handleSubmit = async () => {
  if (!formRef.value) return
  
  await formRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        skillForm.tags = JSON.stringify(tags.value)
        await publishSkill(skillForm)
        ElMessage.success('发布成功')
        router.push('/skill')
      } catch (error) {
        console.error('发布失败：', error)
      } finally {
        loading.value = false
      }
    }
  })
}
</script>

<style scoped>
.publish-container {
  min-height: 100%;
  padding: 30px 20px;
  animation: fadeIn 0.6s ease-out;
}

.page-header {
  text-align: center;
  margin-bottom: 40px;
}

.page-title {
  font-size: 32px;
  font-weight: 800;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
}

.title-icon {
  font-size: 36px;
  color: #667eea;
}

.page-subtitle {
  font-size: 16px;
  color: #666;
  margin: 0;
}

.form-container {
  max-width: 900px;
  margin: 0 auto;
  background: white;
  padding: 50px;
  border-radius: 20px;
  box-shadow: 0 8px 32px rgba(102, 126, 234, 0.12);
  border: 1px solid rgba(102, 126, 234, 0.1);
}

.form-container :deep(.el-form-item__label) {
  font-weight: 700;
  color: #333;
  font-size: 15px;
}

.form-container :deep(.el-input__wrapper),
.form-container :deep(.el-textarea__inner),
.form-container :deep(.el-select) {
  border-radius: 12px;
}

.form-container :deep(.el-input__wrapper) {
  transition: all 0.3s;
  box-shadow: 0 2px 8px rgba(102, 126, 234, 0.08);
}

.form-container :deep(.el-input__wrapper:hover),
.form-container :deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 4px 16px rgba(102, 126, 234, 0.15);
}

.form-container :deep(.el-textarea__inner) {
  box-shadow: 0 2px 8px rgba(102, 126, 234, 0.08);
  transition: all 0.3s;
}

.form-container :deep(.el-textarea__inner:hover),
.form-container :deep(.el-textarea__inner:focus) {
  box-shadow: 0 4px 16px rgba(102, 126, 234, 0.15);
}

.cover-uploader {
  border: 2px dashed #d9d9d9;
  border-radius: 16px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: all 0.3s;
  background: linear-gradient(135deg, rgba(102, 126, 234, 0.03) 0%, rgba(255, 255, 255, 1) 100%);
}

.cover-uploader:hover {
  border-color: #667eea;
  box-shadow: 0 8px 24px rgba(102, 126, 234, 0.15);
}

.cover-uploader-icon {
  font-size: 48px;
  color: #667eea;
  width: 240px;
  height: 180px;
  line-height: 180px;
  text-align: center;
  transition: all 0.3s;
}

.cover-uploader:hover .cover-uploader-icon {
  transform: scale(1.1);
}

.cover-image {
  width: 240px;
  height: 180px;
  display: block;
  object-fit: cover;
  transition: transform 0.6s;
}

.cover-uploader:hover .cover-image {
  transform: scale(1.05);
}

.upload-tip {
  font-size: 13px;
  color: #999;
  margin-top: 12px;
  font-weight: 500;
}

.form-container :deep(.el-tag) {
  border-radius: 12px;
  padding: 6px 14px;
  font-weight: 600;
}

.form-container :deep(.el-button) {
  border-radius: 12px;
  font-weight: 600;
  padding: 12px 28px;
  transition: all 0.3s;
}

.form-container :deep(.el-button--primary) {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
}

.form-container :deep(.el-button--primary:hover) {
  transform: translateY(-3px);
  box-shadow: 0 8px 24px rgba(102, 126, 234, 0.4);
}

.form-container :deep(.el-input-number) {
  border-radius: 12px;
}

.form-container :deep(.el-input-number .el-input__wrapper) {
  border-radius: 12px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .publish-container {
    padding: 20px 12px;
  }

  .page-title {
    font-size: 24px;
  }

  .title-icon {
    font-size: 28px;
  }

  .page-subtitle {
    font-size: 14px;
  }

  .form-container {
    padding: 30px 20px;
    border-radius: 12px;
  }

  .cover-uploader-icon {
    width: 200px;
    height: 150px;
    line-height: 150px;
  }

  .cover-image {
    width: 200px;
    height: 150px;
  }
}
</style>

